package cn.ks.web.controller;

import cn.ks.domain.Lssue;
import cn.ks.domain.LssueLog;
import cn.ks.domain.Purchase;
import cn.ks.exception.GlobalException;
import cn.ks.feign.CgService;
import cn.ks.service.ILssueComeoutService;
import cn.ks.domain.LssueComeout;
import cn.ks.query.LssueComeoutQuery;
import cn.ks.result.JSONResult;
import cn.ks.result.PageList;
import cn.ks.service.ILssueLogService;
import cn.ks.service.ILssueService;
import cn.ks.service.impl.LssueLogServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/lssueComeout")
public class LssueComeoutController {

    @Autowired
    public ILssueComeoutService lssueComeoutService;

    @Autowired
    public ILssueService lssueService;

    @Autowired
    private CgService cgService;
    @Autowired
    private ILssueLogService lssueLogService;

    @Value("${number}")
    private int number;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody LssueComeout lssueComeout) {
        Lssue lssue = new Lssue();
        lssue.setGoodsName(lssueComeout.getName());
        lssue.setGoodsTypeId(lssueComeout.getGoodsTypeId());
        lssue.setCount(lssueComeout.getCount());
        Lssue lssue1 = lssueService.selectByGoodId(lssueComeout.getGoodsTypeId());
        //入库传入商品名字和商品数量 商品id
        lssueComeoutService.insert(lssueComeout);
        //入库
        if (lssueComeout.getState() == 0) {
            if (lssue1 != null) {
                lssue1.setCount(lssue1.getCount() + lssue.getCount());
                lssueService.updateById(lssue1);
            } else {
                lssueService.insert(lssue);
            }
        } else {//出库
            if (lssue1 != null) {
                //库存不足或到了阈值
                if (lssue1.getCount() < number || lssue1.getCount() - lssue.getCount() < 0) {
                    //采购 加日志
                    LssueLog lssueLog = new LssueLog();
                    lssueLog.setLog("库存不足或到了阈值");
                    lssueLog.setGoodsTypeId(lssue1.getGoodsTypeId());
                    lssueLogService.insert(lssueLog);

                    Purchase purchase = new Purchase();
                    purchase.setCount(100L);
                    purchase.setGoodsName(lssueComeout.getName());
                    purchase.setGoodsId(lssueComeout.getGoodsTypeId());
                    purchase.setExamine(1L);
                    cgService.saveOrUpdate(purchase);
                    return JSONResult.error("库存过少无法出库系统将自动采购");
                }
                lssue1.setCount(lssue1.getCount() - lssue.getCount());
                lssueService.updateById(lssue1);
            } else {
                throw new GlobalException("没有该库存,无法出库");
            }
        }
        return JSONResult.success();
    }

    /**
     * 删除对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        lssueComeoutService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(lssueComeoutService.selectById(id));
    }


    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(lssueComeoutService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody LssueComeoutQuery query) {
        Page<LssueComeout> page = new Page<LssueComeout>(query.getPage(), query.getRows());
        page = lssueComeoutService.selectPage(page);
        return JSONResult.success(new PageList<LssueComeout>(page.getTotal(), page.getRecords()));
    }
}
